package cn.likecy.sqlapp.util;

import cn.hutool.core.io.FileUtil;
import cn.likecy.sqlapp.domain.PhotoMini;

import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class ImgUtil {

    /**
     * 照片对象bite[]保存为图片到相应文件夹
     *
     * @param photo
     * @param baseParentDir
     * @param infoVal
     * @return
     */
    public static Boolean GenImageByObjToFilePath(PhotoMini photo, String baseParentDir) {

        String imgFileParentDir =baseParentDir+File.separator+ photo.getPropertyid();
        if (!FileUtil.exist(imgFileParentDir)) {
            FileUtil.mkdir(imgFileParentDir);
        }
        String imgFilePath =imgFileParentDir+ File.separator+ photo.getPhotoid()+".jpg";
        try
        {
            if(photo.getPhotodata()==null){
                return false;
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(photo.getPhotodata());
            out.flush();
            out.close();
            return true;

        }
        catch (Exception e)
        {
            return false;
        }
    }

}
